<?php
/**
 * Created by PhpStorm.
 * User: yulin
 * Date: 2018-7-10
 * Time: 14:36
 */
namespace app\open\model;

use app\open\filter\CarScrapList;
use app\base\exception\RecordNotFoundException;
use think\Db;
use traits\model\SoftDelete;
class CarScrap extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 报废列表
     * @param string $fields
     * @param array $where
     * @param int $page
     * @param int $num
     * @return array
     */
    public function scrapList($fields='*',$where=[],$page=1,$num=10){
        $filter = new CarScrapList();
        $filter->analyzeField();
        list(,$msg)      = $filter->checkField($fields);
        if($where['car_scrap_id']){
            $where2['car_scrap_id'] = $where['car_scrap_id'];
        }
        if($where['company_name']){
            $where2['company_name'] = $where['company_name'];
        }
        if($where['number']){
            $where2['number'] = $where['number'];
        }
        if($where['state']){
            $where2['state'] = $where['state'];
        }
        list(,$whereMsg) = $filter->checkWhere($where2);
        $whereMsg['a.delete_time'] = ["null"];

        $buildQuery = function($query) use ($whereMsg) {
            self::authCompanyQuery($query, $whereMsg, 'a.company_id');
        };

        $data['data'] = Db::name('car_scrap')
            ->alias('a')
            ->join('car c','a.car_id = c.car_id','left')
            ->join('car_brand d','c.car_brand_id = d.car_brand_id','left')
            ->join('car_type h','c.car_type_id = h.car_type_id','left')
            ->join('company i','a.company_id = i.company_id','left')
            ->field($msg)
            ->where($buildQuery)
            ->order('date desc')
            ->page($page,$num)
            ->select();
        $data['count'] = count($data['data']);
//        $data['data']        = $filter->checkData($data['data']);
        $data['description'] = $filter->title;
        $data['fields']      = $filter->fieldTitle;
        return [1,$data];
    }
    /**
     * 新增或者编辑拍卖申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function scrapEdit($params) {
        $data = [];

        $where = [];
        $params['car_scrap_id'] && $where['car_scrap_id'] = $params['car_scrap_id'];

        $update = [];
        $params['why'] && $update['why'] = $params['why'];
        $params['provincial_administration_result'] && $update['provincial_administration_result'] = $params['provincial_administration_result'];
        $params['bureau_of_finance_result'] && $update['bureau_of_finance_result'] = $params['bureau_of_finance_result'];
        $params['car_id'] && $update['car_id'] = $params['car_id'];
        $params['scrap_document'] && $update['scrap_document'] = $params['scrap_document'];
        $params['disposition_of_assets'] && $update['disposition_of_assets'] = $params['disposition_of_assets'];

        if($where['car_scrap_id']){

            $car_scrap_return = Db::name('car_scrap')->where($where)->find();

            if($update['provincial_administration_result'] == '9' || $update['bureau_of_finance_result'] == '9'){
                $update['state'] = '3';
                $data['data'] = Db::name('car_scrap')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //省管局审批
            elseif($update['provincial_administration_result'] == '1' && $car_scrap_return['state'] < '3'){
                if($car_scrap_return['bureau_of_finance_result'] == '1') {
                    $update['state'] = '4';
                }
                else{
                    $update['state'] = '2';
                }
                $update['provincial_administration_result_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_scrap')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //财政局审批
            elseif($update['bureau_of_finance_result'] == '1' && $car_scrap_return['state'] < '3'){
                if($car_scrap_return['provincial_administration_result'] == '1') {
                    $update['state'] = '4';
                }
                else{
                    $update['state'] = '2';
                }
                $update['bureau_of_finance_result_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_scrap')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //报废文件
            elseif($update['scrap_document'] && $car_scrap_return['state'] == '4'){
                $update['state'] = '5';
                $update['scrap_document_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_scrap')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
            //资产处置批复
            elseif($update['disposition_of_assets'] && $car_scrap_return['state'] == '5'){
                $update['state'] = '6';
                $update['disposition_of_assets_date'] = date("Y-m-d H:i:s");
                $data['data'] = Db::name('car_scrap')->where($where)->update($update);
                $data['count'] = $data['data'];
                return [1,$data];
            }
        }
        else if($update['car_id']){
            $car_return = Db::name('car')->where('car_id',$update['car_id'])->find();
            $update['scrap_no'] = $this->createSn();
            $update['state'] = 1;
            $update['date'] = date("Y-m-d H:i:s");
            $update['company_id'] = $car_return['company_id'];
            $data['data'] = Db::name('car_scrap')->insert($update);
            //车辆状态改为8:报废
            Db::name('car')->where('car_id', $update['car_id'])->update(['state' => 8]);
            return [1,$data];
        }
        else{
            $data = ['where' => $where, 'update' => $update , 'params' => $params];
            return [0,$data];
        }
    }

    /**
     * 删除报废申请
     * @param $params
     * @return $this|false|int
     * @throws RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function scrapDelete($id = '') {
        $data = [];
        $car_scrap_return = Db::name('car_scrap')->where('car_scrap_id',$id)->update(['delete_time' => time()]);
        if($car_scrap_return) {
            $car_id = Db::name('car_scrap')->where('car_scrap_id', $id)->value('car_id');
            if($car_id) {
                $car_return = Db::name('car')->where('car_id', $car_id)->update(['state' => '1']);
            }
            $data['count'] = $car_scrap_return;
            return [1, $data];
        }
        else{
            $data['msg'] = '删除失败,查无此car_scrap_id';
            return [0, $data];
        }
    }
}